package com.ethink.platform.controller.mdm;

import com.ethink.framework.common.web.BaseController;
import com.ethink.framework.common.web.response.CommonResponse;
import com.ethink.mdm.api.MateFacade;
import com.ethink.mdm.api.component.MdmComponent;
import com.ethink.mdm.api.info.MateSkuInfo;
import com.ethink.mdm.api.request.GroupMateMateSkuQueryRequest;
import com.ethink.mdm.api.request.MateSkuQueryRequest;
import com.ethink.platform.controller.mdm.info.MateSkuSaleDetailInfo;
import com.ethink.warehouse.api.component.WarehouseComponent;
import com.ethink.warehouse.api.info.MateSkuStockInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品主数据 Controller
 *
 * @author default
 * @date 2022-09-26
 */
@Tag(name = "MateController", description = "商品主数据 Controller")
@RestController
@Slf4j
@RequestMapping("platform-server/v1/mdm-service")
public class MateController extends BaseController {

    @Autowired
    private MdmComponent mdmComponent;

    @Autowired
    private WarehouseComponent warehouseComponent;

    @Autowired
    private MateFacade mateFacade;

    @Operation(summary = "分页查询")
    @GetMapping("/mateSku/query")
    public CommonResponse<List<MateSkuInfo>> queryMateSku(MateSkuQueryRequest request) {
        return succeedWithPage(mateFacade.queryMateSku(request));
    }

    @Operation(summary = "根据sku查询销售商品列表")
    @GetMapping("/mateSkuForSale/listBySkuCode")
    public CommonResponse<List<MateSkuSaleDetailInfo>> listMateSkuForSaleBySkuCode(@RequestParam("mateSkuCodeList") List<String> mateSkuCodeList) {
        if (CollectionUtils.isEmpty(mateSkuCodeList)) {
            return succeedWith(new ArrayList<>());
        }
        List<MateSkuInfo> mateSkuInfoList = mateFacade.queryMateSkuBySkuCodeList(mateSkuCodeList);
        Map<String, MateSkuStockInfo> stockInfoMap = warehouseComponent.queryMateSkuStore(mateSkuCodeList);
        List<MateSkuSaleDetailInfo> detailInfoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(mateSkuInfoList)) {
            for (MateSkuInfo mateSkuInfo : mateSkuInfoList) {
                MateSkuSaleDetailInfo detailInfo = new MateSkuSaleDetailInfo();
                BeanUtils.copyProperties(mateSkuInfo, detailInfo);
                MateSkuStockInfo skuStockInfo = stockInfoMap.get(mateSkuInfo.getMateSku());
                detailInfo.setTotalQty(skuStockInfo == null ? 0 : skuStockInfo.getTotalQty());
                detailInfo.setPurchasingQty(skuStockInfo == null ? 0 : skuStockInfo.getPurchasingQty());
                detailInfoList.add(detailInfo);
            }
        }
        return succeedWith(detailInfoList);
    }

    @Operation(summary = "根据sku查询销售商品详情")
    @GetMapping("/mateSkuForSale/getBySkuCode/{mateSkuCode}")
    public CommonResponse<MateSkuSaleDetailInfo> getMateSkuForSaleBySkuCode(@PathVariable("mateSkuCode") String mateSkuCode) {
        MateSkuInfo mateSkuInfo = mateFacade.getMateSkuBySkuCode(mateSkuCode);
        MateSkuSaleDetailInfo detailInfo = new MateSkuSaleDetailInfo();
        BeanUtils.copyProperties(mateSkuInfo, detailInfo);
        MateSkuStockInfo skuStockInfo = warehouseComponent.queryMateSkuStore(mateSkuCode);
        detailInfo.setTotalQty(skuStockInfo == null ? 0 : skuStockInfo.getTotalQty());
        detailInfo.setPurchasingQty(skuStockInfo == null ? 0 : skuStockInfo.getPurchasingQty());
        return succeedWith(detailInfo);
    }

}